<%@page import="phion.onlineexam.bean.StaticResources"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
	<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
<link href="${APP_PATH }/static/assets/css/bootstrap.min.css"
	rel="stylesheet" type="text/css">
<script src="${APP_PATH }/static/assets/js/jquery.min.js"></script>
<script src="${APP_PATH }/static/assets/js/bootstrap.min.js"></script>
<style type="text/css">
.container {
	display: grid;
	grid-template-columns: repeat(2, 170px);
	justify-items: stretch;
	height: 50px;
	margin: 50px;
}

.container::before {
	display: none;
}

.btn {
	color: white;
	text-decoration: none;
	cusor: pointer;
	background: #0066ff;
	border-radius: 4px;
}

.items {
	margin-top: 10px;
	height: 30px;
}

.a-upload {
	padding: 4px 10px;
	height: 30px;
	line-height: 20px;
	position: relative;
	cursor: pointer;
	color: white;
	background: #0066ff;
	border: 1px solid #ddd;
	border-radius: 4px;
	overflow: hidden;
	display: inline-block;
	*display: inline;
	*zoom: 1
}

.a-upload  input {
	position: absolute;
	font-size: 100px;
	right: 0;
	top: 0;
	opacity: 0;
	filter: alpha(opacity = 0);
	cursor: pointer
}
</style>


</head>
<script>
        　　function checkLeave(){
        	alert("???");
        　　　　event.returnValue="确定离开当前页面吗？";
        　　}
 </script>
<body unbeforeunload="return checkLeave()">
	<c:if test="${isEdit}">
		正在编辑考试...
	</c:if >
	<div>

		<form action="teacher_check_exam" method="post"
			enctype="multipart/form-data" id="form1">
			<a href="javascript:;" class="a-upload" id="a1"> 
				<input type="file" name="paper" id="paperBtn" onchange="ipt1()">
				<font id="paper">点击选择试卷</font>
			</a> 
			<a href="javascript:;" class="a-upload" id="a2"> 
				<input type="file" name="studentOrder" id="studentOrderBtn" onchange="ipt2()">
				<font id="studentOrder">点击选择名单</font>
			</a>
		</form>


		<form action="" class="container" method="get" id="form2"
			style="margin: 0px, auto;">
			<font class="items">考试名称</font>
			<input class="items" id="eName" type="text" name="eName" /> 
			<font class="items">班级</font> 
			<input class="items" id="stuClass" type="text" name="stuClass" /> 
			
			<font class="items" >开始时间</font>
			<input class="items" id="startTime" 
			name="startTime" type="datetime-local" value="2019-04-13T08:00" /> 
			
			<font class="items" >结束时间</font>
			<input class="items" id="endTime" 
			name="endTime" type="datetime-local" value="2019-04-13T10:00" /> 
			
			<input type="button" class="items btn" id="save_as_exam_btn" value="发布考试" />
		</form>
	</div>
<script type="text/javascript">
	//更新日期
	refresh();
	
	var formdata;
	//正式保存考试
	$("#save_as_exam_btn").click(function(){
		var flag = true;
		<c:if test="${isEdit}">
			 var msg = "更新考试表将删除原来的学生信息，是否继续？"; 
			 if (confirm(msg)==true){ 
			   	flag=true; 
			 }else{
				 flag = false;
			 }
		</c:if >
		if(!flag) return;
		alert("正在更新，，，请稍等！5秒后没有回应请重新提交！");
		//1、获取当前已有考试的值
		if(!check()) return;
		formdata = new FormData(document.getElementById("form1"));
		formdata.append("eName",$("#eName").val());
		formdata.append("stuClass",$("#stuClass").val());
		formdata.append("startTimeStr",$("#startTime").val());
		formdata.append("endTimeStr",$("#endTime").val());
		formdata.append("isEdit","${isEdit}");
		formdata.append("eId","${eId}")
		//formdata.append("studentOrder");
		//2、验证并保存考试
		$.ajax({
			url	 :"${APP_PATH}/teacher_validate_exam?isEditStr=${isEdit}",
			type : "POST",
			data :formdata,
			processData:false,
            contentType:false,
			success : function(result) {
				if(result.code==100){
					//alert(result.msg);
					//信息有效，保存考试
					saveExam();
				}else{
					showInfos(result);
				}
			}
		});
	});
	//检查信息
	function check() {
		var res = true;
		var resMsg = "";
		var len;
		if((len=$("#eName").val().length)<2||$("#eName").val.length>6) {
			res = false;
			text = $("#eName").val;
			resMsg +="考试名称长度不能小于2，不能大于6:"+len+'\n';
		}
		if((len=$("#stuClass").val.length)<1||$("#stuClass").val.length>6) {
			res = false;
			resMsg +="班级名称长度不能小于1不能大于6:"+len+'\n';
		}
		var sLen = $("#startTime").val.length;
		var eLen = $("#endTime").val.length;
		if(sLen<=0||eLen<=0||sLen>20||eLen>20) {
			res = false;
			resMsg +="考试时间有误！"+'\n';
		}
		resMsg+="请重新输入！";
		if(!res) alert(resMsg);
		return res;
	}
	
	//保存考试
	function saveExam() {
		$.ajax({
			url	 :"${APP_PATH}/teacher_save_exam",
			type : "POST",
			data :formdata,
			processData:false,
            contentType:false,
			success : function(result) {
				alert(result.msg);
			}
		});
	}
	
	//提示信息
	function showInfos(result) {
		alert(result.msg);
	}
	
	//上传试卷与学生名单
	function ipt1() {
		var paper = document.getElementById("paper");
		var paperBtn = document.getElementById("paperBtn");
		var arr = paperBtn.value.split('\\');
		var fileName = arr[arr.length - 1];
		paper.innerHTML = "<font id='paper'>" + fileName + "</font>";
	};
	function ipt2() {
		var studentOrder = document.getElementById("studentOrder");
		var studentOrderBtn = document.getElementById("studentOrderBtn");
		var arr = studentOrderBtn.value.split('\\');
		var fileName = arr[arr.length - 1];
		studentOrder.innerHTML = "<font id='studentOrder'>" + fileName + "</font>";
	};
	
	function refresh() {
		//希望每次打开都要默认为今天日期+00:00
		//默认时间
		var myDate = new Date(); 
		var Y = myDate.getFullYear();
		var	M = myDate.getMonth() + 1; 
		var	D = myDate.getDate();
		var H = myDate.getHours();
		var m = myDate.getMinutes();
		//处理月是一位的情况
		if((M + '').length == 1){
			M = '0' + (M + '');
		}
		//处理日是一位的情况
		if((D + '').length == 1){
			D = '0' + (D + '')
		}
		var curDay = Y + '-' + M + '-' + D;
		var curTime = H+':'+m;
		$('#startTime').val(curDay +'T'+curTime);
		$('#endTime').val(curDay +'T'+curTime);
		
		//alert(curTime);
	}
	
</script>

</body>
</html>